home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** File: Menu.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1993 Apple Computer, Inc.
- ** All rights reserved.
- */
-
- /* You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes. */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "App.h" /* Get the application includes/typedefs, etc. */
- #include "App.defs.h" /* Get various application definitions. */
- #include "App.protos.h" /* Get the prototypes for application. */
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __TREEOBJ2__
- #include "TreeObj2.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern Boolean gQuitApplication;
- extern Boolean gHasAppleEvents;
- extern OSType gAppWindowType;
-
- extern Boolean gLowOnMem;
- extern short gDialogErr;
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* •• Called by DTS.Lib..framework. •• */
-
- /* Adjust the menu items. We allow the DTS.Lib framework to do most of the work
- ** for us. It will walk the menubar, and for each menu in the menubar, it will
- ** disable all of the menu items and then call the application at either
- ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
- ** or DialogAdjustMenuItems() (for modal dialogs). The application's job is to then
- ** turn on menu items that should be enabled to match the current application state.
- ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
- ** file menu, and DoAdjustEditMenu() for the edit menu. Any other menus that may
- ** be added to Wannabe have all menu items enabled. This allows menus to be added
- ** to the running version of Wannabe and allows them to actually do something.
- ** If the top-most window is a dialog, then all menus are disabled except for the
- ** edit menu. Various items in the edit menu are enabled, depending on if there
- ** is an active TextEdit control, and what is in the clipboard. */
-
- #pragma segment Menu
- void DoAdjustMenus(void)
- {
- if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
- DrawMenuBar();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This is called when an item is chosen from the menu bar (after calling
- ** MenuSelect or MenuKey). It performs the right operation for each command.
- ** It is good to have both the result of MenuSelect and MenuKey go to one
- ** routine like this to keep everything organized. */
-
- #pragma segment Menu
- Boolean DoMenuCommand(short menuID, short menuItem)
- {
- short saveMode, daRefNum, i;
- Str255 str;
- FileRecHndl frHndl, newFrHndl;
- WindowPtr window, clipWind;
- TreeObjHndl root, cobj;
- OSErr err;
- Boolean handled;
-
- handled = true;
-
- window = FrontWindowOfType(kwIsDocument, true);
- if (window)
- frHndl = (FileRecHndl)GetWRefCon(window);
- /* frHndl is valid only if it is one of our windows. */
-
- switch (menuID) {
-
- case mApple:
- switch (menuItem) {
- case kStdAbout: /* Bring up alert for About. */
- NewDocumentWindow(nil, 'ABOT', false);
- break;
- default: /* All non-About items in this menu are DAs. */
- GetMenuItemText(GetMenuHandle(mApple), menuItem, str);
- daRefNum = OpenDeskAcc(str);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case kStdNew:
- gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
- if (gDialogErr)
- NewDocumentWindow(nil, 'ERR#', false);
- break;
- case kStdOpen:
- err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kStdClose:
- DisposeOneWindow(window, kClose);
- break;
- case kStdSave:
- case kStdSaveAs:
- saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- saveMode = kSaveAs;
- err = SaveDocument(frHndl, window, saveMode);
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kDuplicate:
- gDialogErr = DuplicateDocument(frHndl, &newFrHndl);
- if (!gDialogErr) {
- gDialogErr = DoNewWindow(newFrHndl, nil,
- FrontWindowOfType(kwIsDocument, true),
- (WindowPtr)-1);
- if (gDialogErr)
- DisposeDocument(newFrHndl);
- }
- if (gDialogErr)
- NewDocumentWindow(nil, 'ERR#', false);
- break;
- case kStdPageSetup:
- DoSetCursor(&qd.arrow);
- PresentStyleDialog(frHndl);
- break;
- case kStdPrint:
- DoSetCursor(&qd.arrow);
- err = noErr;
- if (!(*frHndl)->d.doc.fhInfo.printRecValid)
- err = PresentStyleDialog(frHndl);
- if (!err) {
- err = PrintDocument(frHndl, true, true);
- PrintDocument(nil, false, false);
- }
- if ((err) && (err != userCanceledErr)) {
- gDialogErr = err;
- NewDocumentWindow(nil, 'ERR#', false);
- }
- break;
- case kStdQuit:
- gQuitApplication = DisposeAllWindows();
- break;
- }
- break;
-
- case mEdit: /* Call SystemEdit for DA editing & MultiFinder. */
- if (!IsDAWindow(window)) {
- if (menuItem == kClipboard) {
- clipWind = GetNextWindow(nil, kClipboardFileType);
- if (clipWind) {
- ShowHide(clipWind, !((WindowPeek)clipWind)->visible);
- CleanSendInFront(clipWind, FrontWindowOfType(kwIsDocument, false));
- }
- break;
- }
- #if VH_VERSION
- if (menuItem == kStdViewHier) {
- handled = false; /* Let framework automatically open the view hierarchy window. */
- break;
- }
- #endif
- switch ((*frHndl)->fileState.sfType) {
- case kDocFileType:
- switch (menuItem) {
- case kStdUndo:
- case kStdRedo:
- DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
- break;
- case kStdCut:
- case kStdCopy:
- case kStdPaste:
- DoClipboard(frHndl, menuItem);
- break;
- case kStdClear:
- DoDelete(frHndl);
- break;
- case kSelectAll:
- BeginContent(window);
- root = (*frHndl)->d.doc.root;
- for (i = (*root)->numChildren; i;) {
- cobj = GetChildHndl(root, --i);
- DoTreeObjMethod(cobj, SETSELECTMESSAGE, SELECTON);
- }
- EndContent(window);
- break;
- default:
- handled = false;
- }
- break;
- #if VH_VERSION
- case kViewHierFileType:
- BeginContent(window);
- switch (menuItem) {
- case kStdUndo:
- CTEUndo();
- break;
- case kStdCut:
- case kStdCopy:
- case kStdPaste:
- case kStdClear:
- CTEClipboard(menuItem - kStdCut + 2);
- break;
- }
- EndContent(window);
- break;
- #endif
- }
- }
- else SystemEdit(menuItem - 1);
- break;
-
- case mArrange:
- DoArrange(frHndl, menuItem);
- break;
-
- case mOther:
- switch (menuItem) {
- case kPenSize:
- NewDocumentWindow(&frHndl, 'PENS', false);
- break;
- case kBorderColor:
- case kContentColor:
- ChangeColor(frHndl, window, menuItem);
- break;
- }
- break;
-
- case mToolPalette:
- switch (menuItem) {
- case -1:
- handled = false;
- break;
- default:
- SetPaletteTool(menuItem - 1);
- break;
- }
-
- default:
- handled = false;
- break;
- }
-
- return(handled);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustFileMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl;
- short enableItem;
-
- menu = GetMenuHandle(mFile);
-
- EnableItem(menu, UnmapMItem(mFile, kStdQuit)); /* Gotta be able to quit. */
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose)); /* Let DAs do a close from the menu. */
- return(false);
- }
-
- if (!gLowOnMem) {
- EnableItem(menu, UnmapMItem(mFile, kStdNew));
- EnableItem(menu, UnmapMItem(mFile, kStdOpen));
- }
-
- window = FrontWindowOfType(kwIsDocument, true);
- if (window) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose));
- frHndl = (FileRecHndl)GetWRefCon(window);
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- enableItem = GetWindowDirty(window);
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- enableItem = true;
- if (enableItem)
- EnableItem(menu, UnmapMItem(mFile, kStdSave));
- EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
- EnableItem(menu, UnmapMItem(mFile, kDuplicate));
- }
- EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
- EnableItem(menu, UnmapMItem(mFile, kStdPrint));
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustEditMenu(WindowPtr window)
- {
- MenuHandle menu;
- Boolean menuEnabled;
- FileRecHndl frHndl, frClip;
- WindowPtr clipWind;
- short i, undoDepth, numUndos;
- Boolean haveDocument;
- Str255 str;
- StringPtr cptr;
- TreeObjHndl root;
- static Str32 clipboardText[3];
-
- menu = GetMenuHandle(mEdit);
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
- EnableItem(menu, UnmapMItem(mEdit, kStdCut));
- EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
- EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
- EnableItem(menu, UnmapMItem(mEdit, kStdClear));
- return(false);
- }
-
- haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
- frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
-
- #if VH_VERSION
- if (haveDocument)
- EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
- #endif
-
- EnableItem(menu, UnmapMItem(mEdit, kClipboard));
- if (!clipboardText[0][0]) {
- GetMenuItemText(menu, UnmapMItem(mEdit, kClipboard), str);
- p2c(str);
- for (i = 0; str[i]; ++i)
- if (str[i] == ',')
- str[i] = 0;
- for (cptr = str, i = 0; i < 3; ++i) {
- c2p((char *)cptr);
- pcpy(clipboardText[i], cptr);
- cptr += (cptr[0] + 1);
- }
- }
- clipWind = GetNextWindow(nil, kClipboardFileType);
- if (clipWind) {
- i = (((WindowPeek)clipWind)->visible) ? 1 : 0;
- pcpy(str, clipboardText[i]);
- pcat(str, clipboardText[2]);
- SetMenuItemText(menu, UnmapMItem(mEdit, kClipboard), str);
- }
-
- if (haveDocument) {
- root = (*frHndl)->d.doc.root;
- switch ((*frHndl)->fileState.sfType) {
- case kDocFileType:
- GetUndoInfo(frHndl, &undoDepth, &numUndos);
- if (undoDepth)
- EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
- if (undoDepth < numUndos)
- EnableItem(menu, UnmapMItem(mEdit, kStdRedo));
- if (mDerefRoot(root)->numSelected) {
- EnableItem(menu, UnmapMItem(mEdit, kStdCut));
- EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
- EnableItem(menu, UnmapMItem(mEdit, kStdClear));
- }
- if (clipWind) {
- frClip = (FileRecHndl)GetWRefCon(clipWind);
- if ((*((*frClip)->d.doc.root))->numChildren)
- EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
- }
- if (mDerefRoot(root)->numSelected < (*root)->numChildren)
- EnableItem(menu, UnmapMItem(mEdit, kSelectAll));
- break;
- #if VH_VERSION
- case kViewHierFileType:
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- #endif
- default:
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustArrangeMenu(WindowPtr window)
- {
- MenuHandle menu;
- short i, numChildren, numSelected, cnum, flag;
- Boolean haveDocument;
- FileRecHndl frHndl;
- TreeObjHndl root, chndl;
-
- if (IsDAWindow(window)) return(false);
-
- menu = GetMenuHandle(mArrange);
-
- haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
- frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
-
- for (i = kMoveForward; i <= kUngroup; ++i) CheckItem(menu, i, false);
- if (haveDocument) {
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- root = (*frHndl)->d.doc.root;
- numChildren = (*root)->numChildren;
- numSelected = mDerefRoot(root)->numSelected;
- if (numSelected) {
- for (cnum = (*root)->numChildren; cnum;) {
- chndl = GetChildHndl(root, --cnum);
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- if ((*chndl)->type == GROUPOBJ)
- EnableItem(menu, kUngroup);
- }
- }
- if ((numSelected) && (numChildren > 1)) {
- if ((flag = numSelected) == 1) {
- chndl = GetChildHndl(root, 0);
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- --flag;
- }
- if (flag) {
- EnableItem(menu, kMoveForward);
- EnableItem(menu, kMoveToFront);
- }
- if ((flag = numSelected) == 1) {
- chndl = GetChildHndl(root, -1); /* Last child. */
- if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
- --flag;
- }
- if (flag) {
- EnableItem(menu, kMoveBackward);
- EnableItem(menu, kMoveToBack);
- }
- }
- if (numSelected > 1)
- EnableItem(menu, kGroup);
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustOtherMenu(WindowPtr window)
- {
- MenuHandle menu;
- Boolean haveDocument;
- FileRecHndl frHndl;
-
- if (IsDAWindow(window)) return(false);
-
- menu = GetMenuHandle(mOther);
-
- haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
- frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
-
- if (haveDocument) {
- EnableItem(menu, kPenSize);
- EnableItem(menu, kBorderColor);
- EnableItem(menu, kContentColor);
- }
-
- return(false);
- }
-
-
-
-